<template>
  <div class="record-wrapper">
    <div class="record-header-wrapper">
      <div class="record-header-search-wrapper">
        <span class="record-header-search-span">选择课程 : </span>
        <el-select class="record-header-search-selector"
                   v-model="selectedCourseId"
                   @change="onCourseChange"
                   placeholder="请选择课程">
          <el-option
                  v-for="item in courses"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </div>
    </div>
    <div class="record-table-wrapper">
      <el-table
              :data="records"
              v-loading="loading"
              element-loading-text="拼命加载中"
              stripe
              style="width: 100%">
        <el-table-column
                prop="course.name"
                label="课程"
                width="200">
        </el-table-column>
        <el-table-column
                prop="teacher.name"
                label="老师名称"
                width="150">
        </el-table-column>
        <el-table-column
                prop="studentName"
                label="学生名称"
                width="150">
        </el-table-column>
        <el-table-column
                prop="status"
                label="考勤状态"
                width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '出勤'" type="success" effect="dark">出勤</el-tag>
            <el-tag v-if="scope.row.status === '旷课'" type="danger" effect="dark">旷课</el-tag>
          </template>
        </el-table-column>
        <el-table-column
                prop="recordTime"
                label="考勤时间">
        </el-table-column>
      </el-table>
      <div class="record-table-pagination-wrapper">
        <el-pagination
                background
                @current-change="currentChange"
                @size-change="sizeChange"
                layout="sizes, prev, pager, next, jumper, ->, total, slot"
                :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "AttdRecord",
    data() {
      return {
        studentIdByUser: null,
        records: [],
        departments: '',
        recordName: '',
        loading: false,
        title: '',
        dialogFormVisible: false,
        selectedCourseId: '',
        total: null,
        size: 10,
        currentPage: 1,
        courses: []
      }
    },
    mounted() {
      this.initStudentIdByUser()
      this.getRecords()
      this.getCourses()
    },
    methods: {
      //  获取当前操作系统的学生的id
      initStudentIdByUser() {
        let user = JSON.parse(window.sessionStorage.getItem('user'))
        this.studentIdByUser = user.studentId
        console.log(this.studentIdByUser)
      },
      //  获取所有考勤记录
      getRecords() {
        this.loading = true
        this.getRequest('/attdmanagement/student/?currentPage=' + this.currentPage
            + '&size=' + this.size
            + '&studentId=' + this.studentIdByUser).then(resp => {
          if (resp) {
            this.records = resp.data
            this.total = resp.total
            this.loading = false
            console.log(this.records)
          }
        })
      },
      //  当分页组件的每页显示多少条数据改变时
      sizeChange(size) {
        this.size = size
        this.currentPage = 1
        this.getRecords()
      },
      //  当分页组件的页数改变时
      currentChange(currentPage) {
        this.currentPage = currentPage
        this.getRecords()
      },
      //  当选择的课程变化时重新获取考勤记录
      onCourseChange() {
        this.loading = true
        this.getRequest('/attdmanagement/student/?currentPage=' + this.currentPage
            + '&size=' + this.size
            + '&studentId=' + this.studentIdByUser
            + '&courseId=' + this.selectedCourseId).then(resp => {
          if (resp) {
            this.records = resp.data
            this.total = resp.total
            this.loading = false
          }
        })
      },
      //  获取课程
      getCourses() {
        this.getRequest('/attdmanagement/student/course').then(resp => {
          if (resp) {
            this.courses = resp
          }
        })
      }
    }
  }
</script>

<style lang="less">

  .record-wrapper {
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .record-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .record-header-search-wrapper {
        display: flex;

        .record-header-search-input {
          margin-right: 10px;
        }

        .record-header-search-button {
          height: min-content;
        }

      }
    }

    .record-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;

      .record-table-pagination-wrapper {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>